﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMessaging.MessagingEx.Common;

namespace MMessaging.YieldTest
{
	class Program
	{
		static void Main(string[] args)
		{
			var messages = new List<int>()
				              {
					              1,
					              2,
					              3,
					              4,
					              5
				              };

			var service = new MMessagingHandler<int>(Handler);

			foreach (var message in messages)
				service.Process(message);
		}

		private static IEnumerable<MMessageProcessing<int>> Handler(MMessagingHandlerContext<int> context)
		{
			if (context.CurrentMessage == 1)
			{

				int a = 10;

				yield return context.WaitOn("Handler() 1", () => context.CurrentMessage == 2);
				yield return context.WaitOn("Handler() 2", () => context.CurrentMessage == 2);
				yield return context.Call("Handler() 3", DoSth);
				yield return context.WaitOn("Handler() 4", () => context.CurrentMessage == 2);
				yield return context.WaitOn("Handler() 5", () => context.CurrentMessage == 3);

				yield return context.Call("Handler() 6", SubHandler);

				a = 20;
			}
		}

		private static IEnumerable<MMessageProcessing<int>> DoSth(MMessagingHandlerContext<int> context)
		{
			yield break;
		}


		private static IEnumerable<MMessageProcessing<int>> SubHandler(MMessagingHandlerContext<int> context)
		{
			int w = 20;

			yield return context.WaitOn("SubHandler() 1", () => context.CurrentMessage == 5);

			int a = w;
		}
	}
}
